# encoding:utf-8
import json
import requests
import requests
import base64
from flask import Flask,render_template, request
# host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=KCOVZYWcPWn7acmbGILERNWZ&client_secret=qHyxDbhbZoqqsUd82OBdorNkyy2Tr7XG'
# response = requests.get(host)
# if response:
#     print(response.json())
# '''
# 通用物体和场景识别
# '''
#
#
# request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
# # 二进制方式打开图片文件
# f = open('5b7dd659e16093522cdd9b4916275cba.jpeg', 'rb')
# img = base64.b64encode(f.read())
#
# params = {"image":img}
# access_token='24.cec258d3b29b727c11999e37a83b84d3.2592000.1629011234.282335-24558785'
# request_url = request_url + "?access_token=" + access_token
# headers = {'content-type': 'application/x-www-form-urlencoded'}
# response = requests.post(request_url, data=params, headers=headers)
# if response:
#     print(response.json())
#     dicts = response.json()
#     response = dicts['result'][0]['keyword']
#     print(dicts['result'][0]['keyword'])


'''
通用物体和场景识别
'''
def main(filename):
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=KCOVZYWcPWn7acmbGILERNWZ&client_secret=qHyxDbhbZoqqsUd82OBdorNkyy2Tr7XG'
    response = requests.get(host)
    if response:
        print(response.json())

    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
    # 二进制方式打开图片文件

    f = open('./static/img/'+filename, 'rb')
    print(filename)
    img = base64.b64encode(f.read())

    params = {"image":img}
    access_token='24.cec258d3b29b727c11999e37a83b84d3.2592000.1629011234.282335-24558785'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print(response.json())
        dicts = response.json()
        response = dicts['result'][0]['keyword']
        print(dicts['result'][0]['keyword'])
        return response

if __name__ == '__main__':
    main()
